package example.objectecho;

import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

public class ObjectEchoServerHandler extends ChannelInboundHandlerAdapter {

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		System.out.println("server active");
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		// drop msg

		
		// System.out.println("direct:" + bf.isDirect());
		// System.out.println( "msg : "+ bf.toString(CharsetUtil.UTF_8));
		ctx.write(msg);
		ctx.write("over");
		 if(msg instanceof List){
			 System.out.println( "is list");
			 List listMsg = ((List) msg);
			 System.out.println("size：" + listMsg.size() + " first element:" + listMsg.get(0));
		 }
		 
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		// TODO Auto-generated method stub
		cause.printStackTrace();
		ctx.close();
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		System.out.println("read complete");
		ctx.flush();
	}
	
	
}
